package com.hsogoo.game.server.handler;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.alibaba.fastjson.JSON;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.hsogoo.game.context.GameContext;
import com.hsogoo.game.entity.Game;
import com.hsogoo.game.entity.Player;
import com.hsogoo.game.entity.Room;
import com.hsogoo.game.msg.JoinRoomMessage;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GameEventHandler {

    protected SocketIOServer server;

    public GameEventHandler(SocketIOServer server) {
        this.server = server;
    }

    @OnConnect
    public void onConnect(SocketIOClient client) {
        log.info("client connected");
    }

    //添加@OnDisconnect事件，客户端断开连接时调用，刷新客户端信息
    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        log.info("client disconnect");
        UUID uuid = client.getSessionId();
        for(Map.Entry<String, Game> entry : GameContext.gameContext.entrySet()){
            Game game = entry.getValue();
            for(Room room : game.getRoomList()){
                boolean notify = false;
                Iterator<Player> iterator = room.getPlayerList().iterator();
                while(iterator.hasNext()){
                    Player player =  iterator.next();
                    if(uuid.equals(player.getClient().getSessionId())){
                        iterator.remove();
                        notify = true;
                    }
                }
                if(notify){
                    for(Player player : room.getPlayerList()){
                        player.getClient().sendEvent("gameStatus", room);
                    }
                }
            }
        }

    }

    @OnEvent(value = "joinRoom")
    public void joinRoom(SocketIOClient client , String data){
        log.info(data);
        JoinRoomMessage message = JSON.parseObject(data , JoinRoomMessage.class) ;
        String gameName = message.getGameName();
        Game game = GameContext.gameContext.get(gameName);
        if(game == null){
            game = new Game();
            game.setGameName(message.getGameName());
            game.setGid("123");
        }
        boolean join = false;
        Player player = new Player();
        player.setName(message.getName());
        player.setUid(message.getUid());
        player.setClient(client);
        List<Room> roomList = game.getRoomList();
        String rid = null;
        for(Room room : roomList){
            if(room.getPlayerList().size() < 3){
                room.getPlayerList().add(player);
                rid = room.getRid();
                GameContext.roomContext.put(rid, room);
                join = true;
            }
        }
        if(!join){
            rid = String.valueOf(roomList.size() + 1);
            Room room = new Room(rid);
            room.getPlayerList().add(player);
            GameContext.roomContext.put(room.getRid(), room);
            roomList.add(room);
        }

        //设置到上下文中
        GameContext.gameContext.put(gameName, game);
        log.info(message.getName() + " join room success");
        Room room = GameContext.roomContext.get(rid);
        List<Player> players = room.getPlayerList();
        List<Player> transferPlayerList = new ArrayList<>();
        for(Player p : players){
            Player transferPlayer = new Player();
            transferPlayer.setUid(p.getUid());
            transferPlayer.setName(p.getName());
            transferPlayerList.add(transferPlayer);
        }
        Room messageRoom = new Room(room.getRid());
        messageRoom.setPlayerList(transferPlayerList);
        for(Player p : players){
            p.getClient().sendEvent("gameStatus", messageRoom);
        }
    }


}
